const userService = require('../services/userService');
const { success, successWithData, error } = require('../utils/resultwrapper');
const { assignUserRoles } = require('../services/roleService');

class UserController {
  // 获取用户列表
  async getUserList(req, res) {
    try {
      const { pageNum = 1, pageSize = 10, username, deptId,  status } = req.query;
      const result = await userService.getUserList({ page: parseInt(pageNum), size: parseInt(pageSize), username, deptId, status });
      res.json(successWithData(result));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 获取用户详情
  async getUserById(req, res) {
    try {
      const { id } = req.params;
      const user = await userService.getUserById(id);
      res.json(successWithData(user));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 获取用户表单详情
  async getUserForm(req, res) {
    try {
      const { id } = req.params;
      const userForm = await userService.getUserForm(id);
      res.json(successWithData(userForm));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 创建用户
  async createUser(req, res) {
    try {
      const userData = req.body;
      const result = await userService.createUser(userData);
      
      // 处理角色分配
      if (userData.roleIds) {
        await userService.assignUserRoles(result.id, userData.roleIds);
      }
      res.json(successWithData(result));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 更新用户
  async updateUser(req, res) {
    try {
      const { id } = req.params;
      const userData = req.body;
      const result = await userService.updateUser(id, userData);
      
      // 处理角色分配
      if (userData.roleIds) {
        await userService.assignUserRoles(id, userData.roleIds);
      }
      res.json(successWithData(result));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 删除用户
  async deleteUser(req, res) {
    try {
      const { id } = req.params;
      await userService.deleteUser(id);
      res.json(success());
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 分配用户角色
  async assignUserRoles(req, res) {
    try {
      const { userId } = req.params;
      const { roleIds } = req.body;
      await userService.assignUserRoles(userId, roleIds);
      res.json(success());
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 获取用户角色
  async getUserRoles(req, res) {
    try {
      const { userId } = req.params;
      const roles = await userService.getUserRoles(userId);
      res.json(successWithData(roles));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 获取当前用户信息
  async getCurrentUser(req, res) {
    try {
      const userId = req.user.id;
      const userInfo = await userService.getCurrentUserInfo(userId);
      res.json(successWithData(userInfo));
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 修改用户密码（管理员）
  async updatePassword(req, res) {
    try {
      const { id } = req.params;
      const { password } = req.query;
      await userService.updatePassword(id, password);
      res.json(success());
    } catch (err) {
      res.json(error(err.message));
    }
  }

  // 修改用户密码（需要验证旧密码）
  async changePassword(req, res) {
    try {
      const { id } = req.params;
      const { oldPassword, newPassword } = req.body;
      await userService.changePassword(id, oldPassword, newPassword);
      res.json(success());
    } catch (err) {
      res.json(error(err.message));
    }
  }
}

module.exports = new UserController();